package com.customization.unionpay.market.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.FormUtil;
import weaver.backup.logging.Logger;
import weaver.backup.logging.LoggerFactory;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;

public class MarketActivityEventDao {

    public static final Logger log = LoggerFactory.getLogger(MarketActivityEventDao.class);

    public static void updateSftstx(List<JSONObject> filteredList) {
        StringBuilder updateSql = new StringBuilder();
        updateSql.append("UPDATE ").append(FormUtil.UF_YXHDSXTZ).append(" SET sftstx = '是' WHERE id IN (");

        boolean isFirst = true;
        for (JSONObject o : filteredList) {
            if (!isFirst) {
                updateSql.append(", ");
            } else {
                isFirst = false;
            }
            updateSql.append(o.getString("id"));
        }

        updateSql.append(")");
        try {
            RecordSet rs = new RecordSet();
            rs.execute(updateSql.toString());
        }catch (Exception e) {
            log.error("更新营销活动事项失败");
        }
    }

    /**
     * 根据ID查询
     * @param ids
     * @return
     */
    public List<JSONObject> queryByIds (List<String> ids){
        //查询传过来的要校验的营销活动事项
        StringBuilder  querySql = new StringBuilder();
        //查询出已入账金额不等于已核销金额的
        querySql.append("select id,yxpthdsqm,yrzje,yhxje from ")
                .append(FormUtil.UF_YXHDSXTZ)
                .append(" where id in (");
        ids.forEach(o->{
            querySql.append(o).append(",");
        });
        querySql.deleteCharAt(querySql.length()-1).append(")");
        try {
            RecordSet rs = new RecordSet();
            rs.execute(querySql.toString());
            List<JSONObject> marketActivityEventList = new ArrayList<>();
            while (rs.next()){
                JSONObject o = new JSONObject();
                o.put("id",rs.getInt("id"));
                o.put("yxpthdsqm",rs.getString("yxpthdsqm"));
                o.put("yrzje",rs.getFloat("yrzje"));
                o.put("yhxje",rs.getFloat("yhxje"));
                marketActivityEventList.add(o);
            }
            return marketActivityEventList;
        }catch (Exception e){
            log.error("根据ID查询营销活动事项失败");
        }
        return null;
    }

    public  List<JSONObject> queryBySqm(List<String> sqmList){
        if(CollectionUtil.isEmpty(sqmList)){
            log.error("查询的授权码条件为空");
            return null;
        }
        StringBuilder querySql = new StringBuilder();
        querySql.append("select t1.id,t1.yxpthdsqm,t1.hdmc,t1.ACT_TOTAL_AT,t1.sqr,h.SUBCOMPANYNAME as gs from ")
                .append(FormUtil.UF_YXHDSXTZ).append(" t1 ")
                .append("left join  hrmsubcompany h on t1.gs = h.id ")
                .append(" where t1.yxpthdsqm in (");
        sqmList.forEach(o->{
            querySql.append(o).append(",");
        });
        querySql.deleteCharAt(querySql.length()-1).append(")");
        RecordSet rs = new RecordSet();
        List<JSONObject> resultList = new ArrayList<>();
        try {
            rs.execute(querySql.toString());
            while (rs.next()){
                JSONObject o = new JSONObject();
                o.put("id",rs.getInt("id"));
                o.put("yxpthdsqm",rs.getString("yxpthdsqm"));
                o.put("hdmc",rs.getString("hdmc"));
                o.put("ACT_TOTAL_AT",rs.getString("ACT_TOTAL_AT"));
                o.put("sqr",rs.getString("sqr"));
                o.put("gs",rs.getString("gs"));
                resultList.add(o);
            }
            return resultList;
        }catch (Exception e){
            log.error("根据授权码查询营销活动事项失败");
        }
        return null;
    }

    public  List<String> queryBySftstx(){
        StringBuilder querySql = new StringBuilder();
        querySql.append("select yxpthdsqm,sftstx from ")
                .append(FormUtil.UF_YXHDSXTZ).append(" where sftstx = '是' ");
        RecordSet rs = new RecordSet();
        List<String> resultList = new ArrayList<>();
        try {
            rs.execute(querySql.toString());
            while (rs.next()){
                resultList.add(rs.getString("yxpthdsqm"));
            }
            return resultList;
        }catch (Exception e){
            log.error("查询营销活动事项失败");
        }
        return null;
    }

    public List<JSONObject> queryAll() {
        // 构建查询SQL语句
        String querySql = "SELECT yrzje, yhxje, yxpthdsqm, sqr, bdh FROM uf_yxhdsxtz";

        try {
            RecordSet rs = new RecordSet();
            rs.execute(querySql);


            List<JSONObject> resultList = new ArrayList<>();

            while (rs.next()) {
                JSONObject record = new JSONObject();
                String[] columns = {"yrzje", "yhxje", "yxpthdsqm", "sqr"};
                for (String column : columns) {
                    record.put(column, rs.getString(column));
                }
                resultList.add(record);
            }
            return resultList;
        } catch (Exception e) {
            log.error("查询营销活动事项台账表失败: " + e.getMessage());
            return null;
        }
    }
}
